package com.pzr.soccer.dao.impl;

import com.google.common.annotations.GwtCompatible;
import com.pzr.soccer.consts.GameConst;
import com.pzr.soccer.consts.RedisConst;
import com.pzr.soccer.dao.GameRepositoryHelper;
import com.pzr.soccer.domain.game.Game;
import com.pzr.soccer.domain.game.GameWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @Author pzr
 * @date:2021-12-15-17:36
 * @Description:
 **/
public class GameRepositoryImpl implements GameRepositoryHelper {
    @Autowired
    RedisTemplate<String ,Object> redisTemplate;
    @Override
    public List<GameWrapper> findOnGoingGamesInRedis() {
        return getGameWrappers(GameWrapper.Game_Cycle_Going);
    }

    @Override
    public List<GameWrapper> findComingGamesInRedis() {
        return getGameWrappers(GameWrapper.Game_Cycle_NotStartYet);

    }

    @Override
    public List<GameWrapper> findEndedGamesInRedis() {
        return getGameWrappers(GameWrapper.Game_Cycle_ended);
    }

    protected List<GameWrapper> getGameWrappers(int cycleStatus) {
        List<GameWrapper> wrappers=new ArrayList<>();
        Set<String> keys=redisTemplate.keys(RedisConst.Game_Today_Prefix+"*");
        for(String key: Objects.requireNonNull(keys)){
            GameWrapper wrapper= (GameWrapper) redisTemplate.opsForValue().get(key);
            if(wrapper!=null&&wrapper.getGameCycle()== cycleStatus){
                wrappers.add(wrapper);
            }
        }
        return wrappers;
    }
}
